﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DoAn
{
    public class Army : Object
    {

        public Army()
        {
            mySprites = new List<MySprite>();
            nSprites = 0;
        }


        public void newArmy(Object init, MySprite sprite)
        {
            MySprite temp = new MySprite(SpriteModel);
            //temp.Blood = Maxblood;
            mySprites.Add(temp);
            nSprites++;
            init.mySprites.Add(temp);
            init.nSprites++;
            //Random r = new Random();
            //int x = r.Next(1, 5);
            //Vector2 f = new Vector2();
            if (Game1.mainhome.nSprites > 0)
            {
                if (temp.Flag == 0)//Nếu là địch
                {
                    //switch (x)

                    //{
                    //    case 1: f = new Vector2(Game1.mapwithcells.Topleft.X + MapWithCells.WIDTHMAP / 2, Game1.mapwithcells.Topleft.Y + 50); break;
                    //    case 2: f = new Vector2(Game1.mapwithcells.Topleft.X + MapWithCells.WIDTHMAP - 50, Game1.mapwithcells.Topleft.Y + MapWithCells.HEIGHTMAP / 2); break;
                    //    case 3: f = new Vector2(Game1.mapwithcells.Topleft.X + MapWithCells.WIDTHMAP / 2, Game1.mapwithcells.Topleft.Y + MapWithCells.HEIGHTMAP - 50); break;
                    //    case 4: f = new Vector2(Game1.mapwithcells.Topleft.X + 50, Game1.mapwithcells.Topleft.Y + MapWithCells.HEIGHTMAP / 2); break;
                    //}
                    temp.Position = sprite.Position;
                    temp.Target = Game1.mainhome.mySprites[0].PosCenter;
                }
                else//là mình
                {
                    temp.Position = sprite.Position - new Vector2(10, -10);
                    temp.Target = sprite.Target + Game1.mainhome.mySprites[0].Size / 2;
                }
            }
        }

        public void newArmy(Object init, MySprite sprite, int lv)
        {
            MySprite temp = new MySprite(SpriteModel);
            temp.Level = lv;
            mySprites.Add(temp);
            nSprites++;
            init.mySprites.Add(temp);
            init.nSprites++;
            //Random r = new Random();
            //int x = r.Next(1, 5);
            //Vector2 f = new Vector2();
            if (Game1.mainhome.nSprites > 0)
            {
                if (temp.Flag == 0)//Nếu là địch
                {
                    temp.Position = sprite.Position;
                    temp.Target = Game1.mainhome.mySprites[0].PosCenter;
                }
                else//là mình
                {
                    temp.Position = sprite.Position - new Vector2(10, -10);
                    temp.Target = sprite.Target + Game1.mainhome.mySprites[0].Size / 2;
                }
            }
        }
        public void Go(Vector2[,] a, bool[,] b, int cellwidth, int celllheight)
        {
            for (int i = 0; i < nSprites; i++)
            {
                if (mySprites[i].Life)
                {
                    if (mySprites[i].DistanceGo != new Vector2(0, 0))
                    {
                        mySprites[i].Position += mySprites[i].DistanceGo;
                        //mySprites[i].CreateDistanceGo();
                    }
                    if (FinishGo(mySprites[i].Position, mySprites[i].Target))
                    {
                        mySprites[i].Position = mySprites[i].Target;
                        mySprites[i].DistanceGo = new Vector2(0);
                        if (!mySprites[i].Attack)
                            mySprites[i].DynamicState(0, mySprites[i].Direction);
                        //ô[i,j] đã có người đứng
                        checkCell(mySprites[i].PosCenter, a, b, cellwidth, celllheight);
                    }
                    
                }
            }
        }

        public bool FinishGo(Vector2 pos, Vector2 tar)
        {
            if (pos.X >= tar.X - 2 && pos.X <= tar.X + 2 && pos.Y >= tar.Y - 2 && pos.Y <= tar.Y + 2)
                return true;
            else
                return false;
        }

        private void checkCell(Vector2 v, Vector2[,] a, bool[,] b, int cellwidth, int cellheight)
        {
            v -= Game1.mapwithcells.Topleft;
            int x = (int)v.X;
            int y = (int)v.Y;
            x = x / cellwidth;
            y = y / cellheight;
            if(x >= 0 && x < Game1.nRows && y >=0 && y < Game1.nCols)
                b[y, x] = true;
        }


    }
}
